package cn.school.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.school.common.constant.enums.DataStatusEnum;
import cn.school.common.core.domain.AjaxResult;
import cn.school.system.domain.BiShop;
import cn.school.system.service.IBiShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.school.system.mapper.BiCashApplyMapper;
import cn.school.system.domain.BiCashApply;
import cn.school.system.service.IBiCashApplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提现申请信息Service业务层处理
 *
 * @author asheng
 * @date 2023-12-25
 */
@Service
public class BiCashApplyServiceImpl implements IBiCashApplyService
{
    @Autowired
    private BiCashApplyMapper biCashApplyMapper;
    @Autowired
    private IBiShopService biShopService;

    /**
     * 查询提现申请信息
     *
     * @param id 提现申请信息主键
     * @return 提现申请信息
     */
    @Override
    public BiCashApply selectBiCashApplyById(Long id)
    {
        return biCashApplyMapper.selectBiCashApplyById(id);
    }

    /**
     * 查询提现申请信息列表
     *
     * @param biCashApply 提现申请信息
     * @return 提现申请信息
     */
    @Override
    public List<BiCashApply> selectBiCashApplyList(BiCashApply biCashApply)
    {
        return biCashApplyMapper.selectBiCashApplyList(biCashApply);
    }

    /**
     * 新增提现申请信息
     *
     * @param cashApply 提现申请信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult insertBiCashApply(BiCashApply cashApply) throws Exception
    {
        if(StringUtils.isNotBlank(cashApply.getOpenid())
                && StringUtils.isNotBlank(cashApply.getBankName())
                && StringUtils.isNotBlank(cashApply.getBankCardNumber())
                && StringUtils.isNotBlank(cashApply.getBankAccountName())
                && cashApply.getAmount()!=null) {

            cashApply.setStatus(1);
            cashApply.setCreateTime(new Date());

            //余额验证等
            BiShop shop = biShopService.selectBiShopByOpenid(cashApply.getOpenid());
            if(shop.getShopCashFee()==null || shop.getShopCashFee().compareTo(BigDecimal.ZERO)<=0){
                return AjaxResult.error("可提现金额为0");
            }

            if(shop.getShopCashFee().compareTo(cashApply.getAmount())<0){
                return AjaxResult.error("可提现余额不足，余额："+shop.getShopCashFee());
            }

            biCashApplyMapper.insertBiCashApply(cashApply);

            //减少提现余额金额
            biShopService.subShopCashFee(cashApply.getAmount(), shop.getId());

            return AjaxResult.success("提现成功");
        }else{
            return AjaxResult.error("必填参数不可为空");
        }
    }

    /**
     * 修改提现申请信息
     *
     * @param biCashApply 提现申请信息
     * @return 结果
     */
    @Override
    public int updateBiCashApply(BiCashApply biCashApply)
    {
        return biCashApplyMapper.updateBiCashApply(biCashApply);
    }

    /**
     * 批量删除提现申请信息
     *
     * @param ids 需要删除的提现申请信息主键
     * @return 结果
     */
    @Override
    public int deleteBiCashApplyByIds(Long[] ids)
    {
        return biCashApplyMapper.deleteBiCashApplyByIds(ids);
    }

    /**
     * 删除提现申请信息信息
     *
     * @param id 提现申请信息主键
     * @return 结果
     */
    @Override
    public int deleteBiCashApplyById(Long id)
    {
        return biCashApplyMapper.deleteBiCashApplyById(id);
    }
}
